﻿using Ch.Admin.Bfs.Sms.Sdmx.Common;
using Ch.Admin.Bfs.Sms.Sdmx.Registry;
using Ch.Admin.Bfs.Sms.Sdmx.Registry.Rights;
using Ch.Admin.Bfs.Sms.Sdmx.Registry.Filters;
using Ch.Admin.Bfs.Sms.Sdmx.Utils;
using System;
using System.Collections.Generic;
using System.Data;
using System.IO;
using System.ServiceModel;
using System.Text;
using System.Windows.Forms;
using System.Xml;
using System.Xml.Serialization;

namespace SdmxMl.Module
{
    public class ModuleFilter
    {
        private ISecuredArtefactRegistry artefactRegistry;
        private HashSet<ArtefactProperty> properties;

        public ModuleFilter(ISecuredArtefactRegistry registry)
        {
            artefactRegistry = registry;
            properties = new HashSet<ArtefactProperty>();
            properties.Add(ArtefactProperty.Type);
            properties.Add(ArtefactProperty.Owner);
            properties.Add(ArtefactProperty.Agency);
            properties.Add(ArtefactProperty.Identifier);
            properties.Add(ArtefactProperty.MajorVersion);
            properties.Add(ArtefactProperty.MinorVersion);
        }

        public DataTable ApplyFilter(string type, string owner, string agency, string id, string version)
        {


            // Type like
            IArtefactFilter filter = new ArtefactPropertyFilter(ArtefactProperty.Type, ComparisonOperator.Like,
                NormalizeFilterValue(type)); 
            // And owner like
            filter = new CombinedArtefactFilter(new ArtefactPropertyFilter(ArtefactProperty.Owner, ComparisonOperator.Like, 
                NormalizeFilterValue(owner)), filter, BooleanOperator.And);
            // And agency like
            filter = new CombinedArtefactFilter(new ArtefactPropertyFilter(ArtefactProperty.Agency, ComparisonOperator.Like,
                NormalizeFilterValue(agency)), filter, BooleanOperator.And);
            // And ID like
            filter = new CombinedArtefactFilter(new ArtefactPropertyFilter(ArtefactProperty.Identifier, ComparisonOperator.Like,
                NormalizeFilterValue(id)), filter, BooleanOperator.And);

            // And Major version like
            KeyValuePair<string, string> majorMinor = NormalizeVersionValue(version);
            if (majorMinor.Key.Length > 0)
                filter = new CombinedArtefactFilter( new ArtefactPropertyFilter(ArtefactProperty.MajorVersion, ComparisonOperator.Equal,
                    majorMinor.Key), filter, BooleanOperator.And);
            if (majorMinor.Value.Length > 0)
                filter = new CombinedArtefactFilter(new ArtefactPropertyFilter(ArtefactProperty.MinorVersion, ComparisonOperator.Equal,
                    majorMinor.Value), filter, BooleanOperator.And);


            return ExecuteFilter(filter);
        }

        private DataTable ExecuteFilter(IArtefactFilter filter)
        {
            DataTable tb = null;

            if (filter != null)
            {

                IList<ArtefactInformation> infos = artefactRegistry.Search(properties, new System.Collections.Generic.HashSet<ArtefactText>(), filter);

                //  radGridView.DataSource = infos;
                tb = new DataTable();

                if (infos.Count > 0)
                {
                    foreach (ArtefactProperty p in properties)
                    {
                        tb.Columns.Add(p.ToString(), typeof(string));
                    }

                    foreach (ArtefactInformation info in infos)
                    {
                        DataRow row = tb.NewRow();
                        int col = 0;
                        foreach (ArtefactProperty p in properties)
                        {
                            row[col] = info[p].ToString();
                            ++col;
                        }
                        tb.Rows.Add(row);
                    }
                }
            }
            return tb;
        }

        private string NormalizeFilterValue(string val)
        {
            return string.IsNullOrEmpty(val) ? "%" : val.Replace('*', '%');
        }

        private KeyValuePair<string, string> NormalizeVersionValue(string version)
        {
            string major = string.Empty;
            string minor = string.Empty;
            if (string.IsNullOrEmpty(version) == false)
            {
                string[] arr = version.Split('.');
                if (arr[0].Trim().Length > 0)
                    major = arr[0].Trim();
                if (arr.Length > 1 && arr[1].Trim().Length > 0)
                    minor = arr[1].Trim();
            }
            return new KeyValuePair<string, string>(major, minor);;
        }
    }
}
